<?php
/**
 * 用户模块 后台
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\UserService;

use App\Services\UserService\UserTool;
use DB;
use Illuminate\Http\Request;

class UserBackstage
{

    protected $UserTool;

    public function __construct(UserTool $UserTool)
    {
        $this->UserTool = $UserTool;
    }
    /**
     * 用户列表
     */
    public function userList(Request $request)
    {
        $user_type = $request->get('type'); //0普通 1设计师 2品牌
        if (strlen($user_type) == 0) {
            $lists = DB::table('users')->orderBy('created_at', 'desc')
                ->distinct()->paginate(10);
        } else {
            $lists = DB::table('users')->where('user_type', $user_type)->orderBy('created_at', 'desc')->distinct()->paginate(10);
        }
        return $lists;
    }

    /**
     * 用户详情
     */
    public function detail(Request $request)
    {
        $user_id = $request->get('user_id');
        $userinfo = DB::table('users')->where('user_id', $user_id)->first();
        $userinfo = $this->UserTool->checkHeader($userinfo);
        $user_type = $userinfo->user_type;
        if ($user_type == 1) {
            $userinfo->authentication = $this->UserTool->getAuthenticationToDesigner($user_id);
        } else if ($user_type == 2) {
            $userinfo->authentication = $this->UserTool->getAuthenticationToBrand($user_id);
        } else {
            $userinfo->authentication = '';
        }
        $userinfo->create_num = $this->UserTool->createNum($user_id);
        $userinfo->fans_num = $this->UserTool->fansNum($user_id);
        return $userinfo;
    }

    /**
     * 获取用户ID组
     */
    public function get_user_id_list($user_type)
    {
        $user_ids = DB::table('users')->where([['user_type', '=', $user_type]])->pluck('user_id');
        return $user_ids;
    }
    
}
